{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\softwares\\python\\lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "Some weights of the model checkpoint at bert-base-chinese were not used when initializing BertModel: ['cls.predictions.bias', 'cls.seq_relationship.bias', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.transform.LayerNorm.bias', 'cls.predictions.transform.dense.bias', 'cls.seq_relationship.weight', 'cls.predictions.transform.dense.weight', 'cls.predictions.decoder.weight']\n",
      "- This IS expected if you are initializing BertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
      "- This IS NOT expected if you are initializing BertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Embedding(21128, 768, padding_idx=0)\n",
      "tensor([ 4.2787e-02, -2.3277e-02,  8.0512e-02,  2.2043e-02, -1.4151e-02,\n",
      "        -7.0138e-02,  4.1703e-02, -3.6853e-02, -4.7412e-02,  4.4100e-02,\n",
      "         2.2732e-02, -1.1210e-02, -4.5232e-02,  1.4331e-02,  8.0629e-02,\n",
      "        -3.8830e-02,  7.5159e-03, -2.3773e-02,  2.6654e-02,  4.3470e-02,\n",
      "         4.6538e-02, -3.6887e-02,  1.3263e-02,  1.4717e-02,  4.6378e-02,\n",
      "        -4.6841e-03,  4.5503e-02, -3.8282e-02,  3.1289e-02,  4.7196e-02,\n",
      "        -8.3168e-02,  7.8967e-02, -6.2586e-02,  5.2468e-02, -7.8360e-02,\n",
      "         2.2761e-03,  2.2278e-02, -5.3067e-02,  3.9564e-02,  5.9223e-02,\n",
      "         7.5460e-04,  2.2763e-02, -4.1518e-02, -3.1989e-02,  4.4551e-02,\n",
      "        -3.7684e-02,  3.6068e-02,  3.7963e-02,  2.3621e-02,  2.3683e-02,\n",
      "         3.7063e-02, -3.4685e-02,  3.1750e-02,  3.5312e-02,  2.0227e-02,\n",
      "         6.4115e-02,  4.8592e-02,  3.8361e-02, -4.3590e-02,  5.3403e-02,\n",
      "         2.8880e-02,  2.6801e-02, -2.5742e-02,  1.6832e-02,  6.1733e-02,\n",
      "         5.6049e-02, -1.7782e-02,  5.2793e-02, -4.3997e-02,  3.6380e-02,\n",
      "        -4.3126e-02,  2.3570e-02,  5.5192e-02,  5.9153e-02, -4.5695e-02,\n",
      "        -1.2710e-02, -6.3223e-02,  2.8279e-02, -1.8137e-02, -8.1871e-03,\n",
      "        -1.6154e-02, -6.7969e-02,  1.3111e-02, -4.0228e-02, -1.5931e-02,\n",
      "        -4.6492e-02, -7.8445e-02, -5.2353e-02,  3.6514e-02, -5.3273e-02,\n",
      "        -1.5732e-02, -4.6913e-02, -6.6151e-02, -5.6113e-02, -5.6446e-02,\n",
      "         4.1293e-02,  3.9044e-02,  6.5047e-02, -6.2864e-02,  4.3887e-02,\n",
      "         1.5274e-02, -5.3358e-02,  3.8118e-02,  2.0094e-02, -5.6966e-02,\n",
      "         3.9316e-02,  4.8751e-02,  2.8096e-02, -5.7139e-02,  6.0600e-02,\n",
      "         3.0517e-02, -9.2828e-03, -1.6569e-02,  4.3893e-02, -1.8985e-03,\n",
      "        -3.4664e-02,  5.8608e-02,  9.3416e-02, -2.1826e-02, -1.3661e-02,\n",
      "         2.7905e-02, -6.9753e-02, -5.0186e-02, -3.4705e-02, -6.1496e-03,\n",
      "         3.8850e-02, -1.8781e-02, -3.4929e-02,  2.5160e-02, -3.1744e-02,\n",
      "         3.9993e-02,  1.9229e-02, -5.1316e-02,  6.6193e-02, -3.7969e-02,\n",
      "         8.3846e-02, -4.7813e-02, -6.1685e-02, -7.4748e-02,  2.8692e-02,\n",
      "        -2.6506e-02, -3.3648e-02, -1.3044e-02,  4.1770e-02,  3.7228e-02,\n",
      "        -4.8192e-02, -3.1322e-02, -5.3065e-02, -3.0600e-02, -4.1939e-02,\n",
      "        -1.0745e-02,  3.5349e-02, -6.3515e-02,  5.2580e-02,  1.5628e-02,\n",
      "        -2.5280e-02,  1.6785e-02,  2.9021e-02, -4.5381e-02, -3.2611e-02,\n",
      "        -1.2659e-02,  4.8364e-02,  4.7632e-02,  9.3385e-03, -2.7255e-02,\n",
      "        -2.5037e-02, -9.7167e-03, -3.9788e-02,  3.5433e-02, -6.9981e-02,\n",
      "         4.4408e-02,  2.1046e-02, -2.8179e-02,  3.6984e-02,  2.7022e-02,\n",
      "        -1.4940e-02,  3.0695e-02,  8.1159e-03, -3.8633e-02, -3.3803e-02,\n",
      "        -5.9879e-02,  2.1052e-02,  3.8642e-02,  5.8900e-02,  1.4325e-02,\n",
      "         3.5126e-02, -4.9491e-02, -1.9353e-02, -4.4669e-02,  3.3924e-02,\n",
      "         6.2361e-03,  4.3910e-02, -2.8009e-02,  6.1355e-02,  2.5766e-02,\n",
      "        -6.0854e-02, -5.7722e-02, -3.7933e-02,  5.6964e-02, -3.1105e-02,\n",
      "         1.7734e-02, -4.0632e-02,  5.9723e-02, -5.0925e-02,  4.1779e-02,\n",
      "         3.4834e-02, -4.6492e-02, -6.0507e-02,  4.3953e-02, -4.9873e-02,\n",
      "        -2.5102e-02, -3.2247e-02, -3.8861e-02,  3.9449e-02, -6.7018e-02,\n",
      "         1.5377e-03,  5.0671e-02,  9.3734e-03,  4.7004e-02,  4.7110e-02,\n",
      "         2.1643e-02,  1.6043e-02,  2.7399e-02, -3.8070e-02,  8.1973e-02,\n",
      "         7.4542e-02,  5.8262e-02,  2.1933e-02,  8.2091e-02, -7.2527e-02,\n",
      "         4.0588e-02,  6.2184e-02,  3.4668e-02,  7.6730e-02,  5.1972e-02,\n",
      "         3.8674e-02,  4.0679e-02,  2.2903e-02,  1.0737e-01,  2.4557e-02,\n",
      "         3.5124e-02, -5.2139e-02, -4.2597e-02,  2.4333e-02,  4.9064e-02,\n",
      "        -1.9851e-02, -2.5356e-02,  2.7520e-02,  1.5182e-02,  3.2381e-02,\n",
      "         3.3096e-04, -1.3467e-03, -7.5286e-03, -3.6764e-02, -3.0876e-02,\n",
      "         4.7276e-02, -4.0342e-02, -5.9989e-02,  4.1594e-02,  2.7476e-02,\n",
      "         2.6504e-02,  4.9601e-02, -3.8497e-02, -5.6371e-02,  2.9313e-02,\n",
      "         1.8992e-02, -2.8380e-02,  3.6537e-02,  2.1448e-02, -6.4319e-02,\n",
      "         1.3338e-02,  1.0737e-02, -6.5629e-02,  4.9058e-02, -1.4524e-02,\n",
      "         4.5208e-02,  2.5602e-02, -5.5582e-02, -1.6682e-02, -6.7135e-02,\n",
      "        -5.1136e-02, -7.0002e-02, -4.5799e-03, -5.9791e-02, -6.0633e-02,\n",
      "         2.7805e-02,  3.8258e-02,  3.8329e-02,  2.7791e-02,  5.6909e-02,\n",
      "        -4.5514e-02, -4.2333e-02,  3.4347e-02, -3.8743e-02, -5.2016e-02,\n",
      "         1.7575e-02, -4.1517e-02, -3.2452e-02,  3.5984e-02,  1.4997e-02,\n",
      "        -1.2724e-02,  5.7654e-02,  4.8907e-02, -1.8309e-02,  8.2802e-03,\n",
      "        -1.6199e-02, -4.3674e-02,  6.6692e-02,  5.1848e-02,  5.0457e-02,\n",
      "         1.7409e-02, -8.1229e-02, -3.3012e-02,  1.4593e-02, -3.4598e-02,\n",
      "        -5.2654e-02,  2.3721e-02,  6.1557e-02,  4.0838e-02, -4.7375e-02,\n",
      "         8.2003e-03, -4.7693e-02,  5.3421e-02,  6.6926e-02,  2.9455e-02,\n",
      "         1.1851e-02,  4.1556e-02, -3.5677e-02, -5.0172e-02,  4.6395e-02,\n",
      "         7.4750e-03, -2.4646e-03, -4.7072e-04, -2.1303e-02, -6.2633e-03,\n",
      "         5.9568e-02,  4.5639e-02,  3.9656e-02, -3.6173e-02, -4.6170e-02,\n",
      "         2.8623e-02, -3.1267e-02,  8.2906e-03, -1.1105e-02, -6.3711e-02,\n",
      "        -5.8084e-03,  3.9545e-02,  2.9545e-02, -6.6246e-02, -1.7209e-02,\n",
      "         1.6483e-02,  9.6169e-02, -6.6379e-03,  4.5830e-02, -4.6197e-02,\n",
      "         3.6645e-02,  3.1680e-02,  3.3197e-02,  1.7190e-02, -4.7570e-02,\n",
      "        -4.8258e-02,  4.4630e-02, -3.0280e-02, -4.3436e-02, -5.1896e-02,\n",
      "         3.5462e-02, -7.5907e-02, -5.3942e-02, -4.6417e-02, -5.1586e-02,\n",
      "        -2.3162e-02,  2.6013e-02, -4.4222e-02, -4.8882e-02,  2.1843e-02,\n",
      "        -1.4573e-02, -9.0106e-03, -2.2006e-03,  5.2888e-03, -5.7722e-02,\n",
      "         4.8712e-02, -1.5154e-02,  4.1569e-02,  2.3165e-02, -9.3177e-03,\n",
      "         4.1496e-02, -1.9259e-03,  3.3541e-02, -3.7491e-02, -2.7062e-02,\n",
      "        -5.2505e-02, -3.8512e-03, -2.6347e-02, -3.9798e-03, -4.8746e-02,\n",
      "         1.6251e-02, -2.9820e-02, -1.9606e-02,  5.3829e-02, -4.1917e-02,\n",
      "        -9.7422e-03, -1.9008e-03, -3.7344e-02,  5.5978e-02, -5.7797e-03,\n",
      "         6.7249e-02, -3.2663e-02, -4.2928e-02,  2.2448e-02,  4.8398e-02,\n",
      "        -5.7844e-02,  4.7560e-02,  2.9033e-02,  3.9541e-02, -4.4091e-02,\n",
      "         9.4801e-03,  3.0746e-02,  1.9354e-02, -5.3685e-02,  8.2666e-03,\n",
      "        -2.5112e-02,  2.1925e-02, -2.0252e-02,  6.1515e-02,  4.0067e-02,\n",
      "        -5.7826e-02, -3.9351e-02, -2.7467e-02, -5.0681e-02, -3.3603e-02,\n",
      "         2.6538e-03, -3.1224e-02, -2.9219e-02,  4.1995e-02,  7.1984e-03,\n",
      "         2.4180e-02,  4.1107e-02, -3.8125e-02,  4.0716e-02, -6.3765e-02,\n",
      "         2.4673e-02,  5.9729e-03,  1.3611e-02, -4.5107e-03,  3.8345e-02,\n",
      "        -1.0253e-01,  4.5341e-02, -4.3223e-02,  5.3163e-02, -6.0245e-02,\n",
      "        -4.0548e-02,  2.2549e-02,  4.4690e-02, -1.0256e-01, -9.0144e-03,\n",
      "         3.1766e-02,  8.7164e-02,  2.1759e-02, -5.1106e-02, -2.6551e-02,\n",
      "        -5.5499e-02, -4.9165e-02,  4.1860e-02, -5.1836e-02,  3.3994e-02,\n",
      "        -3.1130e-02,  3.9068e-02,  1.2759e-02, -3.4884e-02, -5.2158e-02,\n",
      "        -2.8411e-02,  1.8125e-02,  3.5854e-02,  3.1471e-02,  1.8906e-03,\n",
      "         6.5655e-02,  2.6852e-02, -4.1864e-02,  4.5770e-02, -6.9300e-02,\n",
      "         3.9640e-02,  2.2696e-02, -3.8093e-02,  3.5073e-02,  9.4611e-03,\n",
      "        -4.5636e-02,  2.8511e-02,  6.1248e-02,  1.6755e-02,  3.2167e-02,\n",
      "         2.8764e-02, -2.7950e-02,  3.3029e-02, -3.4309e-02, -4.4616e-02,\n",
      "         3.2911e-02,  1.8026e-02,  4.1658e-02, -7.5331e-02,  4.8517e-02,\n",
      "        -4.2447e-02,  7.0309e-02, -4.4550e-02, -1.8583e-02,  4.1626e-02,\n",
      "         5.8514e-02, -4.2397e-02, -5.2063e-02,  5.6362e-03, -7.4106e-03,\n",
      "        -4.5695e-02, -4.9851e-02, -5.4953e-02, -4.3616e-02, -4.2512e-02,\n",
      "        -5.7108e-02, -4.1856e-02, -3.4175e-02, -5.2093e-02, -1.6535e-02,\n",
      "        -5.3510e-02, -2.3120e-02, -3.0318e-02, -9.2795e-03, -3.9525e-02,\n",
      "        -2.8509e-02, -5.0716e-02, -5.3024e-02,  3.6434e-02,  4.0596e-03,\n",
      "         8.2536e-02,  1.2676e-02,  5.3417e-02,  2.9075e-02,  3.1900e-02,\n",
      "        -3.5588e-02, -3.3399e-02, -5.3564e-02, -2.2328e-02,  2.5502e-02,\n",
      "        -5.8775e-02, -7.9650e-03,  4.4986e-02,  6.6968e-02, -4.7039e-02,\n",
      "        -1.5929e-02,  2.8594e-02,  4.8781e-02,  1.8040e-02, -2.7583e-03,\n",
      "         6.9879e-02, -2.6911e-02,  3.0968e-02,  2.3269e-03, -3.6020e-02,\n",
      "         6.0623e-02,  1.6684e-02,  2.7629e-02,  2.0880e-02,  4.7500e-02,\n",
      "         5.5959e-02,  3.3183e-02,  3.1533e-04, -7.3059e-02, -5.0944e-02,\n",
      "        -4.5289e-02, -2.9637e-02,  3.2376e-03,  1.6712e-02, -2.1689e-02,\n",
      "         9.4507e-04, -4.9008e-02, -4.6853e-02, -5.6683e-02,  2.3544e-02,\n",
      "        -2.5564e-02, -4.8775e-02, -8.2722e-02, -9.7379e-02, -6.4762e-02,\n",
      "        -7.7841e-02, -2.2032e-02,  4.9252e-02, -2.1708e-02,  4.4558e-02,\n",
      "        -6.1484e-02, -7.1498e-02, -2.3160e-03,  8.7723e-03, -4.8359e-02,\n",
      "         4.8189e-02, -6.4979e-02, -5.0948e-02,  3.0990e-02, -3.2411e-02,\n",
      "        -3.1061e-02,  9.6633e-04, -1.0757e-02,  4.1937e-02,  6.8197e-02,\n",
      "         2.8819e-02, -3.4128e-04,  1.3246e-02,  2.5449e-02,  3.5702e-02,\n",
      "        -4.6168e-02, -2.3803e-02,  4.3072e-02, -3.3798e-02,  3.3021e-02,\n",
      "         3.2348e-02,  4.1925e-02,  2.4571e-02,  2.4109e-02, -3.8300e-03,\n",
      "        -1.3832e-02, -5.6584e-02, -4.3429e-02, -3.0358e-03, -4.8020e-02,\n",
      "        -3.1390e-02, -2.1355e-02,  2.7421e-03,  2.2615e-02, -2.5692e-02,\n",
      "         5.5913e-02, -8.0708e-04, -2.9049e-02,  5.6698e-02, -4.9236e-02,\n",
      "         4.8800e-02, -3.5667e-02,  4.5517e-02,  6.2734e-03, -5.1012e-02,\n",
      "        -4.4837e-03,  2.5926e-02, -4.8272e-02,  1.1039e-03, -2.1617e-02,\n",
      "         5.2466e-02, -1.9455e-02,  3.1592e-02, -2.4721e-02,  3.9411e-02,\n",
      "        -2.7511e-02,  4.8405e-02, -4.7151e-02, -4.5734e-02,  8.2628e-03,\n",
      "        -3.4367e-02,  1.0755e-02, -5.3581e-02, -4.4157e-02,  4.7782e-02,\n",
      "         3.3080e-02, -5.3415e-02, -3.9493e-02, -4.0704e-02,  5.1991e-02,\n",
      "        -6.4205e-02, -2.6546e-02,  1.8445e-02, -1.5016e-02, -1.6491e-02,\n",
      "         5.0934e-02, -4.8472e-02,  6.0754e-02,  2.7525e-02, -5.3113e-02,\n",
      "        -1.9137e-02, -6.1695e-02,  3.6969e-02,  2.9055e-02,  4.0627e-02,\n",
      "         3.6379e-02, -5.2436e-02,  3.8581e-02, -4.6023e-02, -7.9751e-02,\n",
      "         3.0230e-02, -9.9783e-03,  2.9648e-02, -2.3928e-02,  5.0737e-02,\n",
      "         4.3378e-02,  4.4224e-02, -5.1057e-02,  2.2810e-02,  2.0914e-02,\n",
      "        -4.5046e-02,  7.9920e-02, -5.0204e-02, -3.7665e-02, -2.6770e-02,\n",
      "         6.1787e-02,  8.6684e-05,  2.8364e-02, -1.0263e-01,  1.4230e-02,\n",
      "         1.5838e-02,  3.8253e-02, -5.1528e-02,  3.9376e-02,  4.7645e-02,\n",
      "        -4.8679e-02, -2.7684e-02,  2.5552e-02, -3.8574e-02,  5.8926e-02,\n",
      "        -4.2122e-02,  7.5179e-02, -2.7285e-03,  5.9459e-02,  4.3040e-02,\n",
      "        -2.6933e-02,  8.5357e-03, -1.2872e-02, -5.7665e-03,  5.2160e-02,\n",
      "        -7.4120e-02,  7.1035e-02,  2.5052e-02, -2.4391e-02,  1.8024e-03,\n",
      "         3.9822e-02, -1.8895e-02,  3.6992e-02,  4.5105e-02,  4.7823e-02,\n",
      "        -1.8480e-02,  3.5007e-02, -2.9759e-02, -2.2540e-02,  3.9931e-03,\n",
      "         2.7469e-02, -2.8621e-02, -5.2150e-02,  4.5551e-02, -3.7477e-02,\n",
      "         3.0578e-02, -4.5731e-02,  4.1036e-02,  1.3527e-02,  3.6020e-02,\n",
      "         2.7076e-02, -6.5049e-02, -4.7216e-02, -3.0093e-02, -5.7282e-02,\n",
      "         5.6534e-02, -5.4411e-02, -2.8604e-02, -3.5862e-02,  4.6509e-02,\n",
      "         3.6765e-02, -2.1226e-01,  6.3871e-02, -1.7917e-02,  2.4488e-02,\n",
      "         5.1640e-02,  2.3879e-02, -5.2090e-02,  1.2797e-02,  6.4622e-02,\n",
      "        -5.2885e-02,  3.0926e-02, -4.7950e-02], grad_fn=<SelectBackward0>)\n"
     ]
    }
   ],
   "source": [
    "from transformers import BertModel, BertTokenizer\n",
    "\n",
    "# 加载预训练模型和分词器\n",
    "model = BertModel.from_pretrained('bert-base-chinese')\n",
    "tokenizer = BertTokenizer.from_pretrained('bert-base-chinese')\n",
    "\n",
    "# 获取词汇表中“我”的索引\n",
    "word_index = tokenizer.convert_tokens_to_ids('我')\n",
    "\n",
    "# 获取词嵌入矩阵\n",
    "embeddings = model.get_input_embeddings()\n",
    "print(embeddings)\n",
    "\n",
    "# 通过索引获取“我”的词向量\n",
    "word_embedding = embeddings.weight[word_index]\n",
    "print(word_embedding)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
